package com.itheima.app.controller;

import com.itheima.domain.mongo.Movement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class MovementController {

    @Autowired
    private MovementManager movementManager;

    // 发布动态
    @PostMapping("/movements")
    public ResponseEntity saveMovement(Movement movement, MultipartFile[] imageContent) throws IOException {
        return movementManager.saveMovement(movement, imageContent);
    }

    // 查看我的动态
    @GetMapping("/movements/all")
    public ResponseEntity findMyMovement(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "8") Integer pageSize,
            Long userId) {
        return movementManager.findMyMovement(pageNum, pageSize, userId);
    }

    // 查看好友动态
    @GetMapping("/movements")
    public ResponseEntity findFriendMovement(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "8") Integer pageSize) {
        return movementManager.findFriendMovement(pageNum, pageSize);
    }

    // 查看推荐动态
    @GetMapping("/movements/recommend")
    public ResponseEntity findRecommendMovement(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "8") Integer pageSize) {
        // 调用manager
        return movementManager.findRecommendMovement(pageNum, pageSize);
    }

    // 点赞
    @GetMapping("/movements/{id}/like")
    public ResponseEntity saveMovementLike(@PathVariable("id") String movementId) {
        // 调用manager
        return movementManager.saveMovementLike(movementId);
    }

    // 取消点赞
    @GetMapping("/movements/{id}/dislike")
    public ResponseEntity deleteMovementLike(@PathVariable("id") String movementId) {
        // 调用manager
        return movementManager.deleteMovementLike(movementId);
    }

    // 喜欢
    @GetMapping("/movements/{id}/love")
    public ResponseEntity saveMovementLove(@PathVariable("id") String movementId) {
        // 调用manager
        return movementManager.saveMovementLove(movementId);
    }

    // 取消喜欢
    @GetMapping("/movements/{id}/unlove")
    public ResponseEntity deleteMovementLove(@PathVariable("id") String movementId) {
        // 调用manager
        return movementManager.deleteMovementLove(movementId);
    }

    // 根据主键查询动态详情
    @GetMapping("/movements/{id}")
    public ResponseEntity findMovementById(@PathVariable("id") String movementId) {
        // 调用manager
        return movementManager.findMovementById(movementId);
    }

    // 查询动态详情评论列表
    @GetMapping("/comments")
    public ResponseEntity findMovementComment(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "8") Integer pageSize,
            String movementId) {
        // 调用manager
        return movementManager.findMovementComment(pageNum,pageSize,movementId);
    }

    // 发表评论
    @PostMapping("/comments")
    public ResponseEntity saveMovementComment(@RequestBody Map<String,String> map){
        // 接收参数
        String movementId = map.get("movementId");
        String content = map.get("comment");
        // 调用manager
        return movementManager.saveMovementComment(movementId,content);
    }
}
